package org.alex.samples.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.alex.samples.dao.IExamScore;
import org.alex.samples.dao.IExamTestDAO;
import org.alex.samples.dao.IExamTestTypeDAO;
import org.alex.samples.dao.ISubDAO;
import org.alex.samples.dao.ITestType;
import org.alex.samples.dao.TestUtils;
import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.ExamScore;
import org.alex.samples.model.ExamSubjectVO;
import org.alex.samples.model.ExamTest;
import org.alex.samples.model.ExamTestTypeVO;
import org.alex.samples.model.Sub;
import org.alex.samples.model.TestType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TestServiceImpl implements ITestService{
	
	@Autowired
	private IExamTestDAO testDAO;
	
	@Autowired
	private ISubDAO subjectDAO;
	@Autowired
	private IExamTestTypeDAO examTestTypeDAO;
	
	@Autowired
	private IExamScore examScoreDAO;
	
//	@Autowired
//	private ITestType testTypeDAO;
	
	@Override
	public List<ExamTestVO> getTestListBySubjectExample(Integer subId, String id) {
		List<ExamTestVO> testVOList = new ArrayList<ExamTestVO>();
		List<ExamTest> testList = testDAO.getTestListBySubAndTestTypeExample(subId);
		for(ExamTest test: testList){
			ExamTestVO vo = new ExamTestVO();
			BeanUtils.copyProperties(test, vo);
			ExamTestTypeVO testTypeVO = new ExamTestTypeVO();
			BeanUtils.copyProperties(test.getExamTestType(), testTypeVO);
			vo.setExamTestType(testTypeVO);
			ExamSubjectVO examSubjectVO = new ExamSubjectVO();
			BeanUtils.copyProperties(test.getSubject(), examSubjectVO);
			vo.setExamSubject(examSubjectVO);
			testVOList.add(vo);
		}
		ExamScore score = new ExamScore();
		score.setUserId(id);
		score.setSubId(subId);
		score.setExamTime(new Date());
		//如果成功取到试题
		examScoreDAO.save(score);
		examScoreDAO.flush();
		return testVOList;
				
	}
	@Override
	public Map<Integer, List<ExamTestVO>> getTestListBySubject(Integer subId, String userId) {
		//初始化参数
		List<TestType> testTypeList = examTestTypeDAO.loadAll();
		Map<Integer,Integer> testTypeScoreMap = new HashMap<Integer,Integer>();//所有题型各自的分数
		Map<Integer,List<ExamTestVO>> testListMap = new HashMap<Integer,List<ExamTestVO>>();
		//获取所有题型，得到testTypeScoreMap
		if(testTypeList != null){
			for(TestType testType : testTypeList){
				testTypeScoreMap.put(testType.getId(), testType.getTestTypeScore());
				//3.0根据科目获取各个题型所有题目
				List<ExamTest> testList = testDAO.getTestListBySubAndTestType(subId,testType.getId());
				List<ExamTestVO> testVOList = new ArrayList<ExamTestVO>();
				//分别获取各个题型在数据库里的所有题目
				for(ExamTest test:testList){
					ExamTestVO vo = new ExamTestVO();
					BeanUtils.copyProperties(test, vo);
					ExamTestTypeVO testTypeVO = new ExamTestTypeVO();
					BeanUtils.copyProperties(test.getExamTestType(), testTypeVO);
					vo.setExamTestType(testTypeVO);
					testVOList.add(vo);
				}
				testListMap.put(testType.getId(), testVOList);
			}
		}else{
			return null;
		}
		Sub subject = subjectDAO.get(subId);
		if(subject==null){
			return null;
		}
		Double totalTestScore = subject.getTotal_score();
		Integer totalTestCount = subject.getTest_count();
		//1.检验科目总分和总题数是否匹配
		if(!TestUtils.checkSubjectScoreAndCount(totalTestCount, totalTestScore, testTypeScoreMap)){
			return null;
		}
		//2获取各个题型的数量
		Map<Integer,Integer> testTypeCountMap = TestUtils.getTestCountByScore(totalTestCount ,testTypeScoreMap);
		//生成随机题目
		Map<Integer,List<ExamTestVO>> resultTestListMap = new HashMap<Integer,List<ExamTestVO>>();
		for(Integer key:testTypeCountMap.keySet()){
			Integer count = testTypeCountMap.get(key);
			List<ExamTestVO> testList = testListMap.get(key);
			//3.1在里面
			List<Integer> randomTestIdList = TestUtils.getRandomTestIdList(testList,count);
			if(randomTestIdList == null){
				return null;
			}
			//根据多个id获取多个题目
			List<ExamTest> randomTestList = testDAO.getTestListByIdList(randomTestIdList);
			//转换List<ExamTest>==> List<ExamTestVO>
			List<ExamTestVO> testVOList = new ArrayList<ExamTestVO>();
			for(ExamTest test:randomTestList){
				ExamTestVO vo = new ExamTestVO();
				BeanUtils.copyProperties(test, vo);
				ExamTestTypeVO testTypeVO = new ExamTestTypeVO();
				BeanUtils.copyProperties(test.getExamTestType(), testTypeVO);
				vo.setExamTestType(testTypeVO);
				testVOList.add(vo);
			}
			resultTestListMap.put(key, testVOList);
		}
		ExamScore score = new ExamScore();
		score.setUserId(userId);
		score.setSubId(subId);
		score.setExamTime(new Date());
		//如果成功取到试题
		examScoreDAO.save(score);
		examScoreDAO.flush();
		return resultTestListMap;
		
		
	}
	
	
	@Override
	public List<ExamTest> findAll(){
		return testDAO.findAllExamTests();
	}
	@Override
	public void add(ExamTest examTest) throws Exception{
		testDAO.createExamTest(examTest);
	}
	@Override
	public void edit(ExamTest examTest) throws Exception{
		testDAO.editExamTest(examTest);
	}
	@Override
	public void delete(Integer id) throws Exception{
		testDAO.deleteExamTest(id);
	}
	@Override
	public List<ExamTest> findNameAndType(){
		return testDAO.findAllExamTests();
	}
	@Override
	public ExamTestVO getTestForId(Integer testId){
		System.out.println("id:"+testId);
		ExamTest entity = testDAO.get(testId);
		if(entity != null){
			ExamTestVO vo = new  ExamTestVO();
			BeanUtils.copyProperties(entity, vo);
			Sub subject = subjectDAO.getSubById(entity.getSubject().getId());
			if(subject != null){
				vo.setName(subject.getName());
			}
			ExamTestTypeVO typeVO = new ExamTestTypeVO();
			BeanUtils.copyProperties(entity.getExamTestType(), typeVO);
			vo.setExamTestType(typeVO);
			return vo;
		}
		return null;
	}
//	@Override
//	public List<ExamTestTypeVO> getTestType(){
//		List<TestType> testTypeList = testTypeDAO.findTestType();
//		List<ExamTestTypeVO> testTypeVOList = new ArrayList<ExamTestTypeVO>();
//		for(TestType ty : testTypeList){
//			ExamTestTypeVO tyVO = new ExamTestTypeVO();
//			BeanUtils.copyProperties(ty, tyVO);
//			testTypeVOList.add(tyVO);
//		}
//		return testTypeVOList;
//	}
	@Override
	public List<ExamSubjectVO> getSubject(){
		List<Sub> subjectList = subjectDAO.findAllSubs();
		List<ExamSubjectVO> subjectVO = new ArrayList<ExamSubjectVO>();
		for(Sub s : subjectList){
			ExamSubjectVO sVO = new ExamSubjectVO();
			BeanUtils.copyProperties(s, sVO);
			subjectVO.add(sVO);
		}
		return subjectVO;
	}
	@Override
	public void addTest(ExamTestVO examTestVO) throws Exception{
		ExamTest test = new ExamTest();
		BeanUtils.copyProperties(examTestVO, test);
		TestType ty = new TestType();
		ty.setId(examTestVO.getExamTestType().getId());
		test.setExamTestType(ty);
		Sub su = new Sub();
		su.setId(examTestVO.getExamSubject().getId());
		test.setSubject(su);
		System.out.println(test.getId());
		testDAO.editExamTest(test);
	}

	@Override
	public List<ExamTestVO> loadAllTest() {
		List<ExamTest> entities = testDAO.findAllExamTests();
		if(entities != null){
			ExamTestVO vo = null;
			List<ExamTestVO> resultList = new ArrayList<ExamTestVO>();
			for(ExamTest entity : entities){
				vo = new ExamTestVO();
				BeanUtils.copyProperties(entity, vo);
				Sub subject = subjectDAO.getSubById(entity.getSubject().getId());
				if(subject != null){
					vo.setName(subject.getName());
				}
				ExamTestTypeVO typeVO = new ExamTestTypeVO();
				BeanUtils.copyProperties(entity.getExamTestType(), typeVO);
				vo.setExamTestType(typeVO);
				resultList.add(vo);
			}
			return resultList;
		}
		return null;
		
	}


	
	
	

}
